package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.access.RedisUtil;
import com.dudu.bean.DictionaryBean;
import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.enumeration.RedisKeyEnum;
import com.dudu.service.IDictionaryService;
import com.dudu.service.ILogService;
import com.dudu.service.impl.DictionaryServiceImpl;
import com.dudu.service.impl.LogService;

import core.CoreService;
/**
 * 数据字典
 * @author qinfx
 *
 */
public class DictionaryController extends CoreService implements IDictionaryController {
	//当前登陆用户
	private  final String KEY_CURRENT_USER = "key_current_user";
	//当前登陆用户类型
	//private  final String KEY_CURRENT_USER_TYPE = "key_current_user_type";
	private IDictionaryService dictionaryService=new DictionaryServiceImpl();
	//用于记录相应接口调用的日志
	private ILogService logService = new LogService();
	
	/**
	 * 菜单列表
	 * @return
	 */
	public String queryAll() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
        if (currentUser != null) {
        	JSONArray jsonArray = new JSONArray();
        	//查询所有
	        List<JSONObject> list = this.dictionaryService.queryAll();
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", list.size());
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}
	
	/**
	 * 增加菜单
	 * @return
	 */
	public String doInsert() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
        if (currentUser != null) {
            String dataStr = (String) this.getRequest().getParameter("data");
            JSONObject data = JSONObject.fromObject(dataStr);
            String type = data.getString("type");
            String data_key = data.getString("data_key");
            String data_value = data.getString("data_value"); 
            String comments = data.getString("comments"); 

            DictionaryBean bean = new DictionaryBean();
            bean.setType(type);
            bean.setData_key(data_key);
            bean.setData_value(data_value);
            bean.setComments(comments);
            int pId = this.dictionaryService.insertDictionary(bean);//新增
            
            if("redis".equalsIgnoreCase(type)){
            	RedisUtil.setString(data_key, data_value);
            }
            
            logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_dictionary", "type："+type+"|data_key："+data_key+
					"|data_value："+data_value+"|comments："+comments+
					"|主键："+ pId);
            
            object.put("flag", "000000");
	        object.put("desc", "success");
        } else {
			object.put("flag", "100000");
		}

        return returnData(object.toString());
    }
	
	/**
	 * 修改菜单
	 * @return
	 */
	public String doEdit() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
        if (currentUser != null) {
            String dataStr = (String) this.getRequest().getParameter("data");
            JSONObject data = JSONObject.fromObject(dataStr);
            String id = data.getString("id");
            String type = data.getString("type");
            String data_key = data.getString("data_key");
            String data_value = data.getString("data_value"); 
            String comments = data.getString("comments"); 

            if (StringUtils.isNotBlank(id)) {
            	 DictionaryBean bean = new DictionaryBean();
            	 bean.setId(Integer.valueOf(id));
                 bean.setType(type);
                 bean.setData_key(data_key);
                 bean.setData_value(data_value);
                 bean.setComments(comments);
                int pId = this.dictionaryService.updateDictionary(bean);
                
                if("redis".equalsIgnoreCase(type)){
                	RedisUtil.setString(data_key, data_value);
                }
                
                logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), 
                		"t_dictionary", "type："+type+"|data_key："+data_key+
    					"|data_value："+data_value+"|comments："+comments+
    					"|主键："+ pId);
            }
            object.put("flag", "000000");
	        object.put("desc", "success");
        } else {
			object.put("flag", "100000");
		}

        return returnData(object.toString());
	}
	
}
